package com.voda.web.common;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.voda.entity.api.AbstractBaseEntity;
import com.voda.entity.api.IBaseEntity;
import com.voda.web.util.ResourceBundleUtil;

/**
 * The Class AbstractMappingController.
 */
public class AbstractMappingController {
	
	protected static final String CONSUMES_JSON="application/json";
	protected static final String CONTENT_TYPE_JSON_HEADER = "content-type=application/json";
	
	private ObjectMapper mapper;
	
	/** The resource bundle util. */
	protected ResourceBundleUtil resourceBundleUtil = ResourceBundleUtil.getInstance();
	
	public ObjectMapper getMapper() {
		if (null == mapper) {
			mapper = new ObjectMapper();
			mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		}
		return mapper;
	}
	
	protected IBaseEntity generateBaseEntityFromJson(Object data, Class<? extends AbstractBaseEntity> clazz) throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = getMapper();
		String jsonString = mapper.writeValueAsString(data);
		return mapper.readValue(jsonString, clazz);
	}
	
	
}
